package com.knowledge.knowledge_notes.service.impl;

import com.knowledge.knowledge_notes.dao.AdminRoleDAO;
import com.knowledge.knowledge_notes.dao.AdminRolePermissionDAO;
import com.knowledge.knowledge_notes.dao.AdminUserRoleDAO;
import com.knowledge.knowledge_notes.pojo.AdminMenu;
import com.knowledge.knowledge_notes.pojo.AdminPermission;
import com.knowledge.knowledge_notes.pojo.AdminRole;
import com.knowledge.knowledge_notes.pojo.AdminUserRole;
import com.knowledge.knowledge_notes.service.*;
import net.bytebuddy.implementation.bytecode.assign.TypeCasting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class AdminRoleServiceImpl implements AdminRoleService {
    @Autowired
    private AdminRoleDAO adminRoleDAO;

    @Autowired
    AdminRolePermissionDAO adminRolePermissionDAO;

    @Autowired
    private UserService userService;

    @Autowired
    private AdminUserRoleService adminUserRoleService;

    @Autowired
    private AdminUserRoleDAO adminUserRoleDAO;

    @Autowired
    AdminPermissionService adminPermissionService;

    @Autowired
    AdminMenuService adminMenuService;

    @Autowired
    AdminRolePermissionService adminRolePermissionService;

    @Override
    public List<AdminRole> listWithPermsAndMenus() {
        List<AdminRole> roles = adminRoleDAO.findAll();
        List<AdminPermission> perms;
        List<AdminMenu> menus;
        for (AdminRole role : roles) {
            perms = adminPermissionService.listPermsByRoleId(role.getId());
            menus = adminMenuService.getMenusByRoleId(role.getId());
            role.setPerms(perms);
            role.setMenus(menus);
        }
        return roles;
    }

    @Override
    public void addOrUpdate(AdminRole adminRole) {
        adminRoleDAO.save(adminRole);
    }

    @Override
    @Transactional
    public void editRole(@RequestBody AdminRole role) {
        adminRoleDAO.save(role);
        adminRolePermissionService.savePermChanges(role.getId(), role.getPerms());
    }

    @Override
    public void deleteRole(AdminRole requestRole) {
        adminRolePermissionDAO.deleteAllByRid(requestRole.getId());
        adminRoleDAO.delete(requestRole);
    }

    @Override
    public Set<String> queryUserAuths(String username) {
        Set<String> userRoles = listRolesByUserName(username);
        return userRoles;
    }

    @Override
    public List<AdminRole> listRolesByUser(String username) {
        int uid = userService.findByUserName(username).getId();
        List<Integer> rids = adminUserRoleService.listAllByUid(uid)
                .stream().map(AdminUserRole::getRid).collect(Collectors.toList());
        return adminRoleDAO.findAllById(rids);
    }

    @Override
    public Set<String> listRolesByUserName(String username) {
        Set<String> userRoles = new HashSet<>();
        int uid = userService.findByUserName(username).getId();
        List<Integer> rids = adminUserRoleService.listAllByUid(uid)
                .stream().map(AdminUserRole::getRid).collect(Collectors.toList());
        return userRoles;
    }
}
